package com.tjsinfo.tjsoa.message;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import com.tjsinfo.tjsoa.system.vo.User;

import net.sf.jml.Email;
import net.sf.jml.MsnContact;
import net.sf.jml.MsnMessenger;
import net.sf.jml.MsnProtocol;
import net.sf.jml.MsnUserStatus;
import net.sf.jml.event.MsnAdapter;
import net.sf.jml.event.MsnContactListAdapter;
import net.sf.jml.impl.MsnMessengerFactory;

public class JmlSend extends MsnAdapter {
	private static MsnMessenger messenger = null;
	private String email = null, password = null;

	public void login() {
		messenger = MsnMessengerFactory.createMsnMessenger(email, password);// 创建MsnMessenger
		messenger
				.setSupportedProtocol(new MsnProtocol[] { MsnProtocol.MSNP12 });// 设置登录协议
		messenger.getOwner().setInitStatus(MsnUserStatus.ONLINE);// 设置用户状态
		messenger.addListener(this);// 注册事件
		messenger.login();// 登录
		System.out.println("开始登录");
		System.out.println("email=="+email);
		System.out.println("password=="+password);
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		this.contactListInitCompleted(messenger);
	}

	public void contactListInitCompleted(MsnMessenger messenger) {
        //get contacts in allow list
//		System.out.println("----------------------------------");
        MsnContact[] cons = messenger.getContactList().getContacts();
        System.out.println("您现在有："+cons.length+"个联系人,列表如下!");
              for (MsnContact con : cons) {
//                  System.out.println("姓名:"+con.getDisplayName());
//                  System.out.println("email地址:"+con.getEmail());
//                  System.out.println("在线状态:"+con.getStatus());
//                  System.out.println(con.getPersonalMessage());
                  User.getNowMsnUser().put(con.getEmail().toString(), con.getStatus().toString());
              }
    }
	
	public void sendMessage(final Hashtable<String, String> hashtable) {
		List<String> list = new ArrayList<String>();
		for (Iterator iterator = hashtable.keySet().iterator(); iterator.hasNext();) {
			final String  userid = (String) iterator.next();
			if (userid!=null && !"".equals(userid)) {
				final String  emailaddress = userid;  
				final String  value = hashtable.get(userid);
				list.add(userid);
				messenger.addContactListListener(new MsnContactListAdapter() {
		            public void contactListInitCompleted(MsnMessenger messenger) {
		            	if (User.getNowMsnUser().get(emailaddress)!=null) {
		            		if (!MsnUserStatus.OFFLINE.equals(User.getNowMsnUser().get(emailaddress))) {
		            			messenger.sendText(Email.parseStr(emailaddress), value);
		            			User.getSendMessage().remove(emailaddress);
							}
		            	}else {
		            		//User.getSendMessage().remove(emailaddress);
						}
		            }
		        });
			}
		}
		//User.clearTable(list);
	}
	
	public void logout() {
		messenger.logout();
	}
	public void setEmail(String email) {
		this.email = email;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public static MsnMessenger getMessenger() {
		return messenger;
	}
}
